package method;

/**
 * @author DCSGO
 * @version 1.0
 * 线程常用方法2
 * yield join
 */
public class Method02 {
    //yield 线程的礼让 让出 CPU 让其他线程执行 但礼让的时间不确定 也不一定礼让成功
    //join 线程的插队 插队的线程一旦插队成功 则必须等插队的线程执行完毕后其他线程才能执行
    public static void main(String[] args) throws InterruptedException {
        System.out.println("main t2 同时执行");
        T2 t2 = new T2();
        t2.setName("t2");
        t2.start();
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);

            if (i == 5) {
                //System.out.println("t2.join");
                //t2.join();

                System.out.println("Thread.yield");
                Thread.yield();
            }
            Thread.sleep(500);
        }
        /* //t2.join();
         * main t2 同时执行
         * main 0
         * t2 0
         * t2 1
         * main 1
         * t2 2
         * main 2
         * t2 3
         * main 3
         * t2 4
         * main 4
         * t2 5
         * main 5
         * t2.join
         * t2 6
         * t2 7
         * t2 8
         * t2 9
         * main 6
         * main 7
         * main 8
         * main 9
         *
         * 进程已结束，退出代码为 0
         */

        /*
         * main t2 同时执行
         * main 0
         * t2 0
         * t2 1
         * main 1
         * t2 2
         * main 2
         * t2 3
         * main 3
         * t2 4
         * main 4
         * t2 5
         * main 5
         * Thread.yield
         * t2 6
         * main 6
         * t2 7
         * main 7
         * t2 8
         * main 8
         * t2 9
         * main 9
         *
         * 进程已结束，退出代码为 0
         */
    }
}

class T2 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(currentThread().getName() + " " + i);
            try {
                sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
